package com.zxn.service;

import com.zxn.utils.ElasticUtils;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;

/**
 * program: springbootelastic
 * description: query_string 是一个比较有用的查询关键字，因为他可以匹配某些字段或者全部字段
 * author: zxn
 * create: 2019-04-08 22:14
 **/
public class QueryStringLikeService {

    public static void queryString(String prefix){
        prefix = "*" + prefix +"*";
        QueryBuilder qb = QueryBuilders.queryStringQuery(prefix);
        TransportClient client = ElasticUtils.getClient();
        SearchResponse response = client.prepareSearch("highlight").setTypes("folks")
                                        .setQuery(qb)
                                        .get();
        SearchHits hits = response.getHits();
        for (SearchHit hit: hits) {
            System.out.println(hit.getSource());
        }
        System.out.println(hits.totalHits);
    }

    public static void main(String[] args) {
        queryString("3");
    }
}
